package com.jus.player.activity.my;

import android.content.Intent;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AlertDialog;

import com.jus.player.R;
import com.jus.player.activity.BaseActivity;
import com.jus.player.activity.LoginActivity;
import com.jus.player.util.CommonUtils;
import com.jus.player.util.DialogUtils;
import com.jus.player.util.LanguageUtils;

/**
 * 语言设置页面
 */
public class LanguageSettingActivity extends BaseActivity {

    @Override
    protected int initLayout() {
        return R.layout.activity_language_setting;
    }

    @Override
    protected void initView() {
        RelativeLayout back = findViewById(R.id.system_back);
        back.setOnClickListener(view -> finish());

        RelativeLayout simplifiedChinese = findViewById(R.id.zh_cn);
        RelativeLayout traditionalChinese = findViewById(R.id.zh_tw);
        RelativeLayout english = findViewById(R.id.en);
        simplifiedChinese.setOnClickListener(v ->
                CommonUtils.navigateTo(this, LanguageSettingActivity.class));

        simplifiedChinese.setOnClickListener(v -> switchLanguage(LanguageUtils.ZH_CN));
        traditionalChinese.setOnClickListener(v -> switchLanguage(LanguageUtils.ZH_TW));
        english.setOnClickListener(v -> switchLanguage(LanguageUtils.EN));
    }

    @Override
    protected void initData() {
    }

    /**
     * 切换语言
     *
     * @param languageCode 语言码
     */
    private void switchLanguage(String languageCode) {

        // 提示用户重启应用
        DialogUtils.showConfirmDialog(this, getString(R.string.restart),
                getString(R.string.restart_message),
                () -> {
                    LanguageUtils.applyLanguage(this, languageCode);
                    restartApp();
                });
    }

    // 重启应用
    private void restartApp() {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

}